import { _decorator, Component, EventTouch, Input, instantiate, Label, Node, UITransform, Vec2, Vec3 } from 'cc';
import { AssetList, Direction, Global } from '../Global';
import { UIMgr } from '../Mgr/UIMgr';
import { AudioMgr, SoundName } from '../Mgr/AudioMgr';
import { GameSet } from '../Pop/GameSet';
import { EleLoad } from './EleLoad';
import { CatLoad } from './CatLoad';
import { GAMEDATA } from './GAMEDATA';
import { GameEnd } from '../Pop/GameEnd';
import { LocalStorageMgr } from '../Mgr/LocalStorageMgr';
import { TopPrompt } from '../Top/TopPrompt';
const { ccclass, property } = _decorator;

@ccclass('GameScene')
/**
 * 游戏场景
 */
export class GameScene extends Component {

    //
    static Instance: GameScene = null;

    protected onLoad(): void {
        GameScene.Instance = this;
        Global.IsEnd = false;
    }

    //操作背景
    @property(Node)
    private TouchBg: Node = null;

    //精灵元素层
    @property(Node)
    private EleLoad: Node = null;

    //开始点击得位置
    private StartPos: Vec2 = new Vec2(0, 0);

    //是否继续判断
    private IsJudge: boolean = true;

    //判断显示距离
    private CheckDis: number = 50;

    //
    @property(Label)
    private ScoreLabel = null;

    //金币
    @property(Label)
    private CoinLabel: Label = null;

    //猫咪层
    @property(Node)
    public CatLoad: Node = null;

    private IsDoubleClick: boolean = true;
    private IsRanClick: boolean = true;
    private IsClearClick: boolean = true;

    protected start(): void {

        GAMEDATA.CAT_STYLE = [];
        GAMEDATA.IsDouble = false;

        this.IsDoubleClick = false;
        this.IsRanClick = false;
        this.IsClearClick = false;

        for (let catinfo of LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.CATINFO) {
            if (catinfo.ishave) GAMEDATA.CAT_STYLE.push(catinfo.index)
        }

        this.TouchBg.on(Input.EventType.TOUCH_START, this.TouchStart, this);
        this.TouchBg.on(Input.EventType.TOUCH_MOVE, this.TouchMove, this);

        GAMEDATA.GameScore = 0;
        this.UpdateScore(0);
        //初始化游戏难度
        Global.SpriteType = 2;

        this.CatLoad.getComponent(CatLoad).UpdateClearCat();

    }

    /**
    * 点击设置
    */
    private SetClick() {
        AudioMgr.instance.PlayEffect(SoundName.Click);
        let set: Node = UIMgr.Instance.ShowPop(AssetList.Pop.GAMESET);
        set.getComponent(GameSet).Init(false);
    }

    /**
         * 手指触摸屏幕
         */
    public TouchStart(_e: EventTouch) {
        this.StartPos = _e.getUILocation();
        this.IsJudge = true;
    }

    /**
     * 手指移动
     */
    public TouchMove(_e: EventTouch) {
        if (!this.IsJudge) return;

        //横向判断
        if (Math.abs(_e.getUILocation().x - this.StartPos.x) > this.CheckDis) {

            if (_e.getUILocation().x < this.StartPos.x) {
                this.EleLoad.getComponent(EleLoad).SlideControl(Direction.LEFT);
            } else {
                this.EleLoad.getComponent(EleLoad).SlideControl(Direction.RIGHT);
            }

            this.IsJudge = false;

            return;
        }

        //纵向判断
        if (Math.abs(_e.getUILocation().y - this.StartPos.y) > this.CheckDis) {

            if (_e.getUILocation().y < this.StartPos.y) {
                this.EleLoad.getComponent(EleLoad).SlideControl(Direction.DOWN);
            } else {
                this.EleLoad.getComponent(EleLoad).SlideControl(Direction.UP);
            }

            this.IsJudge = false;
        }

    }

    /**
     * 更新游戏分数
     */
    public UpdateScore(_score: number) {
        GAMEDATA.GameScore += _score * (GAMEDATA.IsDouble ? 2 : 1);
        this.ScoreLabel.string = GAMEDATA.GameScore.toString();

        Global.SpriteType = Math.floor(GAMEDATA.GameScore / 50);

        if (Global.SpriteType < 2) Global.SpriteType = 2;
        else if (Global.SpriteType > 6) Global.SpriteType = 6;

    }

    /**
     * 更新金币数量
     */
    public UpdateCoin() {
        this.CoinLabel.string = "X" + GAMEDATA.GameCoin.toString();
    }


    /**
     * 游戏结束
     */
    public GameOver() {
        Global.IsEnd = true;

        let gs: Node = UIMgr.Instance.ShowPop(AssetList.Pop.GAMEEND);
        gs.getComponent(GameEnd).UpdateScore();
    }

    /**
     * 双倍点击
     */
    private DoubleClick() {

        if (this.IsDoubleClick) {
            let ss: Node = UIMgr.Instance.ShowTop(AssetList.Top.Prompt);
            ss.getComponent(TopPrompt).UpdateContent("该道具只能使用一次");
            return;
        }

        this.IsDoubleClick = true;

        AudioMgr.instance.PlayEffect(SoundName.Click);
        let ss: Node = UIMgr.Instance.ShowTop(AssetList.Top.Prompt);
        ss.getComponent(TopPrompt).UpdateContent("积分金币双倍");
        GAMEDATA.IsDouble = true;
    }

    /**
     * 随机点击
     */
    private RandomClick() {

        if (this.IsRanClick) {
            let ss: Node = UIMgr.Instance.ShowTop(AssetList.Top.Prompt);
            ss.getComponent(TopPrompt).UpdateContent("该道具只能使用一次");
            return;
        }

        this.IsRanClick = true;

        AudioMgr.instance.PlayEffect(SoundName.Click);
        let ss: Node = UIMgr.Instance.ShowTop(AssetList.Top.Prompt);
        ss.getComponent(TopPrompt).UpdateContent("重新分配水果");
        this.EleLoad.getComponent(EleLoad).UpdateAll();
    }

    /**
     * 清屏点击
     */
    private ClearClick() {

        if (this.IsClearClick) {
            let ss: Node = UIMgr.Instance.ShowTop(AssetList.Top.Prompt);
            ss.getComponent(TopPrompt).UpdateContent("该道具只能使用一次");
            return;
        }

        this.IsClearClick = true;

        AudioMgr.instance.PlayEffect(SoundName.Click);
        let ss: Node = UIMgr.Instance.ShowTop(AssetList.Top.Prompt);
        ss.getComponent(TopPrompt).UpdateContent("清除屏幕所有水果");
        this.EleLoad.getComponent(EleLoad).ClearAll();
    }

}


